package com.atguigu.springcloud.payment.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.atguigu.springcloud.framework.common.api.ApiCode;
import com.atguigu.springcloud.framework.common.api.ApiResult;
import com.atguigu.springcloud.framework.core.bean.RequestDetail;
import com.atguigu.springcloud.payment.service.PaymentFeginService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author lenovo
 * @version 1.0
 * @date 2020/11/11 11:07
 */
@Slf4j
@RestController
@RequestMapping("order")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class OrderController {

    @Value("${service-url.payment}")
    private String paymentService;

    final RestTemplate restTemplate;


    @GetMapping("/payment/nacos/{id}")
//    @SentinelResource(value = "fallback") //没有配置
//    @SentinelResource(value = "fallback", fallback = "handlerFallback")//fallback只负责业务异常
//    @SentinelResource(value = "fallback", blockHandler = "blockHandler") //blockHandler只负责sentinel控制台配置违规
    @SentinelResource(value = "fallback", fallback = "handlerFallback", blockHandler = "blockHandler")
    public ApiResult<RequestDetail> getPayment(@PathVariable("id") Integer id) {
        ApiResult<RequestDetail> forObject = restTemplate.getForObject(paymentService + "/payment/nacos/" + id, ApiResult.class);
        if (id == 4) {
            throw new IllegalArgumentException("IllegalArgumentException,非法参数异常....");
        } else if (forObject.getData() == null) {
            throw new NullPointerException("NullPointerException,该ID没有对应记录,空指针异常");
        }
        return forObject;
    }

    public ApiResult<RequestDetail> handlerFallback(@PathVariable("id") Integer id, Throwable e) {
        return ApiResult.fail(ApiCode.SYSTEM_EXCEPTION, new RequestDetail().setIp(id + "").setPath("我不晓得，没查到"))
                .setMessage("兜底异常HandlerFallback，id=" + id + "exception内容：" + e.getMessage());
    }

    public ApiResult<RequestDetail> blockHandler(@PathVariable("id") Integer id, BlockException e) {
        return ApiResult.fail(ApiCode.SPRING_BOOT_PLUS_EXCEPTION, new RequestDetail().setIp(id + "").setPath("我不晓得，没查到"))
                .setMessage("blockHandler-sentinel限流，无此流水，id=" + id + ";BlockException：" + e.getMessage());
    }


    @Resource
    private PaymentFeginService paymentFeginService;

    @GetMapping("payment/nacosFegin/{id}")
    public ApiResult<RequestDetail> getPaymentFeign(@PathVariable("id") Integer id){
        return paymentFeginService.getPayment(id);
    }


}
